package com.boot2.core.web.websocket;

import com.boot2.core.HlAssert;
import com.boot2.core.web.BaseController;
import com.boot2.core.web.TokenSessionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author zhangweilin
 * @Description: xxxxx
 * @date 2020/12/15
 */
@RestController
public class WebSocketController extends BaseController {

    @Autowired(required = false)
    TokenSessionContext tokenSessionContext;

    @Autowired
    WebSocketSessionManager webSocketSessionManager;

    @MessageMapping("/ping")
    @SendToUser("/queue/pong")
    public WebSockeHeartBeatVo ping(WebSockeHeartBeatDto webSockeHeartBeatDto) {
        HlAssert.notNull(tokenSessionContext, "不支持的操作，没有实例化tokenSessionContext");
        String name = tokenSessionContext.getName(webSockeHeartBeatDto.getToken());
        webSocketSessionManager.online(name);
        WebSockeHeartBeatVo webSockeHeartBeatVo = WebSockeHeartBeatVo.builder().time(new Date()).build();
        return webSockeHeartBeatVo;
    }
}
